package com.lemon.boot.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lemon.boot.dto.TenantDTO;
import com.lemon.boot.po.TenantPO;
import com.lemon.boot.service.ITenantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;

/**
 * 租户表控制层
 *
 * @author Lemon
 * @since 2024-02-29
 */
@Log4j2
@Api(tags = "租户管理")
@RestController
@RequestMapping("tenant")
@PreAuthorize("hasRole('ROLE_SUPER')")
public class TenantController {
    @Resource
    private ITenantService iTenantService;

    /**
     * 添加单条数据
     *
     * @param tenantPO
     * @return
     */
    @ApiOperation(value = "添加单条数据")
    @PostMapping("add")
    public String add(@RequestBody TenantPO tenantPO) {
        return iTenantService.add(tenantPO);
    }

    /**
     * 根据ID删除数据
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据ID删除数据")
    @DeleteMapping("del/{id}")
    public String del(@PathVariable("id") Long id) {
        return iTenantService.del(id);
    }

    /**
     * 根据ID修改数据（空字段不更新）
     *
     * @param tenantPO
     * @return
     */
    @ApiOperation(value = "根据ID修改数据（空字段不更新）")
    @PutMapping("edit")
    public String edit(@RequestBody TenantPO tenantPO) {
        return iTenantService.edit(tenantPO);
    }

    /**
     * 根据ID查询数据
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据ID查询数据")
    @GetMapping("get/{id}")
    public TenantPO get(@PathVariable("id") Long id) {
        return iTenantService.get(id);
    }

    /**
     * 根据实体类查询分页数据
     *
     * @param tenantDTO
     * @return
     */
    @ApiOperation(value = "根据实体类查询分页数据")
    @GetMapping("page")
    public Page<?> page(TenantDTO tenantDTO) {
        return iTenantService.page(tenantDTO);
    }

    /**
     * 启用
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "启用")
    @PutMapping("enable/{id}")
    public String enable(@PathVariable("id") Long id) {
        return iTenantService.enable(Collections.singletonList(id));
    }

    /**
     * 停用
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "停用")
    @PutMapping("unable/{id}")
    public String unable(@PathVariable("id") Long id) {
        return iTenantService.unable(Collections.singletonList(id));
    }
}